<template>
	<view class="wrap">
		<view class="city-filter">
			<scroll-view scroll-x class="city-scroll" show-scrollbar="false">
				<view class="city-list">
					<view class="city-item" 
						v-for="(item, index) in cityList" 
						:key="index"
						:class="{'active': currentCityId === item.id}"
						@click="onCitySelect(item)">
						<text>{{item.name}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="filter-box">
			<view class="b-item" v-for="(item, index) in groupList" :key="index"
				:class="{'f-active': index == groupIndex}" @click="onGroupSelect(index)">
				<view class="s-tit">{{item.name}}</view>
			</view>
		</view>
		<view class="shop-map" v-if="showMap && shopMarkers.length > 0">
			<map id="map" :latitude="shopMarkers[0].latitude" :longitude="shopMarkers[0].longitude" scale="10"
				:markers="shopMarkers" @markertap="onMarkerTap" @callouttap="onMarkerTap"></map>
		</view>
		<view class="shop-list">
			<view class="b-items" v-if="shopList.length > 0">
				<view class="b-item" v-for="(item, index) in shopList" :key="index" >
					<view class="b-cover" v-if="item.cover_url" @click="onChooseShop(item.id)">
						<image :src="item.cover_url" mode="aspectFill"></image>
					</view>
					<view class="b-main">
						<view class="b-title" @click="onChooseShop(item.id)">
							<view class="s-name">{{item.name}}</view>

						</view>
						<view class="b-label" v-if="item.start_time && item.end_time" @click="onChooseShop(item.id)">
							<view class="s-icon iconfont icon-time-range"></view>
							<view class="s-cont">{{item.start_time}} - {{item.end_time}}</view>
              <view class="s-state f-rest" v-if="item.state == 0">已休息</view>
              <view class="s-state f-ing" v-else-if="item.state == 1">营业中</view>
						</view>
						<view class="b-label f-address" v-if="item.address">
							<view class="s-icon iconfont icon-address"></view>
							<view class="s-cont" @click="onOpenAddress(index)">
								<view>{{item.address}}</view>
<!--								<view class="iconfont icon-direct" v-if="item.longitude && item.latitude"></view>-->
							</view>
						</view>
					</view>
					<view class="b-action" >
            <view class="s-enter"  @click="onChooseShop(item.id)">去下单</view>
            <view style="display: flex">
              <view   @click="onPhoneCall(item.contact)" v-if="item.contact">
                <img class="callphone" src="@/static/imgs/callphone.png"/>
              </view>
              <view   @click="onOpenAddress(index)" v-if="item.longitude && item.latitude">
                <img class="callphone" src="@/static/imgs/location.png"/>
              </view>
            </view>

					</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
		<!-- 扫描二维码按钮 -->
		<view class="scan-btn" @click="onScanQRCode">
			<text class="iconfont icon-scan"></text>
			<text>扫描门店二维码</text>
		</view>
	</view>
</template>

<script>
	import {
		getShopList
	} from '@/service/api/shop.js'
	import {
		getCityList
	} from '@/service/api/city.js'
	import {
		formatDate
	} from '@/common/util.js'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				cityList: [],
				currentCityId: '',
				groupIndex: 0,
				groupList: [{
						name: '全部',
						value: 'all',
					},
					{
						name: '营业中',
						value: 'ing',
					},
					{
						name: '已休息',
						value: 'rest',
					},
				],
				shopMarkers: [],
				shopList: [],
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadCityList()
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadShopList();
			}
		},
		computed: {
			showMap() {
				return this.$store.getters.globalConfig.show_map || 0
			},
			datetime() {
				return (timestamp) => {
					return formatDate(timestamp, 'yyyy-MM-dd hh:mm')
				}
			}
		},
		methods: {
			loadCityList() {
				getCityList().then(([err, res]) => {
					if (!err && res) {
						this.cityList = res
						if (res.length > 0) {
							this.currentCityId = res[0].id
							this.loadShopList(true)
						}
					}
				})
			},
			onCitySelect(city) {
				this.currentCityId = city.id
				this.loadShopList(true)
			},
			loadShopList(refresh) {
				console.log('loadShopList', refresh);
				if (refresh) {
					this.page = 1
					this.shopList = []
					this.shopMarkers = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				const group = this.groupList[this.groupIndex].value
				getShopList(group, this.page, 10, false, this.currentCityId).then(([err, res]) => {
					console.log('getShopList', err, res)
					this.loadingType = -1
					if (!err) {
						if (res.items.length > 0) {
							let shopMarkers = []
							for (let i = 0; i < res.items.length; i++) {
								let shopItem = res.items[i]
								if (!shopItem.latitude || !shopItem.longitude) {
									continue
								}
								shopMarkers.push({
									id: shopItem.id,
									// title: shopItem.name,
									latitude: shopItem.latitude,
									longitude: shopItem.longitude,
									width: 30,
									height: 30,
									iconPath: '/static/imgs/map-marker.png',
									callout: {
										content: shopItem.name,
										display: 'ALWAYS',
										bgColor: '#FF9626',
										color: '#ffffff',
										borderRadius: 15,
										padding: 10
									}
								})
							}
							this.shopList = [...this.shopList, ...res.items]
							this.shopMarkers = [...this.shopMarkers, ...shopMarkers]
							console.log('this.shopMarkers', shopMarkers)
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无数据'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			},
			onGroupSelect(index) {
				this.groupIndex = index
				this.loadShopList(true)
			},
			onMarkerTap(e) {
				console.log('onMarkerTap', e)
				this.onChooseShop(e.detail.markerId)
			},
			onChooseShop(id) {
				console.log('onChoose', id)
				this.$store.commit('setShopId', id)
				this.$store.commit('delDeskId')
				uni.reLaunch({
					url: '/pages/home/index?from=jump'
				})
			},
			onPhoneCall(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			onOpenAddress(index) {
				const shopData = this.shopList[index]
				if (!shopData.latitude || !shopData.longitude) {
					return
				}
				uni.openLocation({
					latitude: shopData.latitude,
					longitude: shopData.longitude,
					name: shopData.name,
					address: shopData.address
				})
			},
			onScanQRCode() {
				// #ifdef MP-WEIXIN
				uni.scanCode({
					success: (res) => {
						let path = res.path
						// 从path中提取门店id
						let shopId = path.split('=').pop()
						this.$store.commit('setShopId', shopId)
						this.$store.commit('delDeskId')
						uni.reLaunch({
							url: '/pages/home/index?from=jump'
						})
					},
					fail: (err) => {
						console.log('扫码失败：', err)
					}
				})
				// #endif
				
				// #ifdef H5
				this.$logic.showToast('请在微信小程序中使用此功能')
				// #endif
			},
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {}

	.city-filter {
		background: #fff;
		padding: 20rpx 0;
		margin-bottom: 20rpx;
		
		.city-scroll {
			width: 100%;
			white-space: nowrap;
			
			.city-list {
				display: inline-flex;
				padding: 0 30rpx;
				
				.city-item {
					padding: 10rpx 30rpx;
					margin-right: 20rpx;
					background: #f5f5f5;
					border-radius: 30rpx;
					font-size: 28rpx;
					color: #666;
					transition: all 0.3s;
					
					&.active {
						background: $pq-active-color;
						color: #fff;
					}
					
					&:last-child {
						margin-right: 0;
					}
				}
			}
		}
	}

	.filter-box {
		margin-top: 20rpx;
		padding: 30upx 50upx 0 50upx;
		background: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.b-item {
			margin: 0 40upx;
			height: 80upx;
			line-height: 80upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30upx;
			padding-bottom: 6upx;

			&.f-active {
				padding-bottom: 0;
				border-bottom: 6upx solid $pq-active-color;

				.s-tit {
					// color: #DA5650;
				}
			}

			.s-tit {
				color: $pq-main-color;
			}
		}
	}

	.shop-map {
		map {
			width: 750upx;
			height: 500upx;
		}
	}

	.shop-list {
		padding: 20upx 20upx 0 20upx;

		.b-items {
			.b-item {
				padding: 20upx;
				margin-bottom: 20upx;
				border-bottom: 1upx solid #eee;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background: #fff;
				border-radius: 20upx;

				&:last-child {
					border-bottom: none;
					margin-bottom: 0;
				}

				.b-cover {
					width: 140upx;
					height: 140upx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 10upx;
					}
				}

				.b-main {
					margin-left: 20upx;
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.b-title {
						margin-bottom: 10upx;
						display: flex;
						align-items: center;

						.s-name {
							color: $pq-main-color;
							font-size: 32upx;
							font-weight: bold;
              text-overflow: ellipsis;
              overflow: hidden;
              width: 340rpx;
						}


					}

					.b-label {
						margin-top: 10upx;
						display: flex;
						align-items: center;

						.s-icon {
							margin-right: 10upx;
							color: $pq-minor-color;
							font-size: 28upx;
						}

						.s-cont {
							color: $pq-minor-color;
							font-size: 24upx;
						}
            .s-state {
              margin-left: 20upx;
              color: $pq-main-color;
              font-size: 24upx;

              &.f-rest {
                color: $pq-minor-color;
              }

              &.f-ing {
                color: #FF9626;
              }
            }

						&.f-address {
							.s-cont {
								display: flex;
								align-items: center;

								.iconfont {
									margin-left: 10upx;
									font-size: 28upx;
									color: $pq-minor-color;
								}
							}
						}
					}
				}


				.b-action {
					padding-left: 20upx;
					border-left: 2upx solid #eee;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-between;


          .callphone{
            width: 50upx;
            height: 50upx;
            margin-right: 10upx;
            margin-left: 10upx;
          }
					.s-enter {
						width: 130upx;
						height: 60upx;
						line-height: 60upx;
						text-align: center;
						color: #fff;
						font-size: 24upx;
						background: $pq-active-color;
						border-radius: 30upx;
            margin-bottom: 20upx;
					}

					.iconfont {
						margin-top: 20upx;
						background: #eee;
						width: 60upx;
						height: 60upx;
						border-radius: 30upx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 32upx;
						color: $pq-active-color;
					}
				}
			}
		}
	}

	.scan-btn {
		position: fixed;
		left: 50%;
		bottom: calc(30upx + env(safe-area-inset-bottom));
		transform: translateX(-50%);
		background: $pq-active-color;
		color: #fff;
		height: 88upx;
		line-height: 88upx;
		border-radius: 44upx;
		padding: 0 60upx;
		font-size: 24upx;
		display: flex;
		align-items: center;
		box-shadow: 0 4upx 16upx rgba(255, 150, 38, 0.3);
		
		.iconfont {
			font-size: 40upx;
			margin-right: 10upx;
		}
		
		&:active {
			transform: translateX(-50%) scale(0.98);
		}
	}
</style>